package org.ilc.task;

import org.ilc.push.Task;

import java.util.concurrent.ThreadPoolExecutor;

/**
 *
 * @author malongbo
 */
public class TaskExecutor extends Thread{
    private ThreadPoolExecutor threadPool = null;
    private Queue queue = null;
    private boolean isWork = false;
    public TaskExecutor(ThreadPoolExecutor threadPool, Queue queue){
        this.threadPool=threadPool;
        this.queue = queue;
    }

    public void startWork () {
        if (this.isWork) return;

        this.isWork = true;
        super.start ();
    }

    @Override
    public void run() {
        isWork = true;
        while (isWork) {
            Task task = queue.getTask ();
            threadPool.execute (new Executor (task));
        }
    }

    /**
     * 任务执行器
     */
    class Executor implements Runnable {
        private Task task;

        public Executor (Task task) {
            this.task = task;
        }

        @Override
        public void run() {
            task.work ();
        }
    }

    public void stopWork () {
        this.isWork = false;
    }

    public boolean isWork () {
        return this.isWork;
    }
}
